package com.gettagger.tagger.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.gettagger.tagger.models.GuardianObject;
import com.gettagger.tagger.models.StudentInfoObject;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class TaggerDataRetriever {

	// private final String rootURL = "http://www.gettagger.com/";
	private final String rootURL = "http://192.168.0.104/tagger/";

	public TaggerDataRetriever() {
		super();
	}

	/**
	 * 
	 * @param username
	 * @param password
	 * @return
	 * 		ArrayList of Guardian Object. It contains information about the account user.
	 * @throws Exception
	 */
	public ArrayList<GuardianObject> verifyAccount(String username, String password) throws Exception {
		ArrayList<GuardianObject> obj = null;
		// Create a new HttpClient and Post Header
		HttpClient httpclient = new DefaultHttpClient();

		HttpPost httppost = new HttpPost(rootURL + "verifier/login");

		// Add data
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
		nameValuePairs.add(new BasicNameValuePair("username", username));
		nameValuePairs.add(new BasicNameValuePair("password", password));
		httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

		// Execute HTTP Post Request
		HttpResponse response = httpclient.execute(httppost);
		HttpEntity entity = response.getEntity();
		String responseText = EntityUtils.toString(entity);

		Gson gson = new Gson();
		obj = gson.fromJson(responseText, new TypeToken<ArrayList<GuardianObject>>() {
		}.getType());
		return obj;
	}

	
	public StudentInfoObject getInfoObj(String postVal) throws Exception {
		StudentInfoObject infoObj=null;
		String responseText = null;
		// Create a new HttpClient and Post Header
		HttpClient httpclient = new DefaultHttpClient();
		
		HttpPost httppost = new HttpPost(rootURL + "verifier/inquireInfo/"+postVal);
		
		// Execute HTTP Post Request
		HttpResponse response = httpclient.execute(httppost);
		HttpEntity entity = response.getEntity();
		responseText = EntityUtils.toString(entity);
		
		Gson gson = new Gson();
		infoObj = gson.fromJson(responseText, StudentInfoObject.class);
		return infoObj;
	}

	

}
